package com.ziyia.wim.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ziyia.wim.pojo.Msg;
import com.ziyia.wim.R;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class MsgAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<Msg> mList;
    private Context context;

    public MsgAdapter(Context context, List<Msg> mList) {
        this.mList = mList;
        this.context = context;
    }

    private final class LeftViewHolder extends RecyclerView.ViewHolder{
        private TextView username;
        private TextView leftMsg;

        public LeftViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            leftMsg = itemView.findViewById(R.id.leftMsg);
            username = itemView.findViewById(R.id.username);
        }
    }
    private final class RightViewHolder extends RecyclerView.ViewHolder{
        private TextView username;
        private TextView rightMsg;

        public RightViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            rightMsg = itemView.findViewById(R.id.rightMsg);
            username = itemView.findViewById(R.id.username);
        }
    }


    @Override
    public int getItemViewType(int position) {
//        Log.d("position", String.valueOf(position));
//        Log.d("消息类型", mList.get(position).toString());
        return mList.get(position).getType();
    }

    @NonNull
    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        Log.d("viewType", String.valueOf(viewType));
        if (viewType == Msg.TYPE_RECEIVED) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_left_item, parent, false);
            return new LeftViewHolder(view);
//            view = View.inflate(context, R.layout.msg_left_item, null);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_right_item, parent, false);
            return new RightViewHolder(view);
//            view = View.inflate(context, R.layout.msg_right_item, null);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull RecyclerView.ViewHolder holder, int position) {
        Msg msg = mList.get(position);
        if (holder instanceof LeftViewHolder) {
            LeftViewHolder left = (LeftViewHolder) holder;
            left.leftMsg.setText(msg.getContent());
            left.username.setText(msg.getUsername());
        } else {
            RightViewHolder right = (RightViewHolder) holder;
            right.rightMsg.setText(msg.getContent());
            right.username.setText(msg.getUsername());
        }
//        holder..setText(msg.getContent() == null ? "年少有为1" : msg.getContent());
//        holder.rightMsg.setText(msg.getContent() == null ? "年少有为2" : msg.getContent());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
}
